package Models;

import DB.DBCheckpoint;
import DB.DBLocation;
import DB.DBRound;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class Round implements DB.RowInterface
{
	private int roundNo;
	private int locationNo;
	private Location location;
	private ArrayList<Checkpoint> checkpoints = new ArrayList<Checkpoint>();

	public Round ()
	{}

	public void importData(ResultSet data) throws SQLException
	{
		this.setRoundNo(data.getInt("roundNo"));
		this.setLocationNo(data.getInt("locationNo"));

		DBLocation dbl = new DBLocation();
		Location locationRow = dbl.fetchByNo(getLocationNo());
		if(locationRow == null) locationRow = new Location();
		this.setLocation(locationRow);
	}
	
	public void fetchSubItems()
	{
		DBCheckpoint dbc = new DBCheckpoint();
		ArrayList<Checkpoint> checkpointRows = dbc.fetchAll("roundNo = "+roundNo);
		if(checkpointRows == null) checkpointRows = new ArrayList<Checkpoint>();
		this.setCheckpoints(checkpointRows);
	}

	public HashMap exportData()
	{
		HashMap data = new HashMap();
		data.put("locationNo", getLocation().getLocationNo());
		return data;
	}

	public void setData(Location location) throws EmptyValueException
	{
		if(location != null)
		{
			this.setLocation(location);
		}
		else
		{
			throw new EmptyValueException();
		}
	}

	public void refreshData()
	{
		Round round = new DBRound().fetchByNo(getRoundNo());
		this.setLocationNo(round.getLocationNo());
		this.setCheckpoints(round.getCheckpoints());
	}

	private double calculateTime()
	{
		//5 minutter per checkpoint
		return checkpoints.size()*(1/60*5);
	}

	public static String[] getSearchColumns()
	{
		return new String[] { "roundNo" };
	}

	public boolean addCheckpoint(Checkpoint checkpoint)
	{
		checkpoint.setRoundNo(getRoundNo());
		return checkpoints.add(checkpoint);
	}

	public boolean removeCheckpoint(Checkpoint checkpoint)
	{
		int i = 0;
		while(i <= getCheckpoints().size())
		{
			if(checkpoint.getCheckpointNo() == getCheckpoints().get(i).getCheckpointNo())
			{
				new DBCheckpoint().deleteByNo(checkpoint.getCheckpointNo());
				return checkpoints.remove(getCheckpoints().get(i));
			}
			i++;
		}
		return false;
	}

	public void setRoundNoOnCheckpoints()
	{
		for(Checkpoint checkpoint : checkpoints)
		{
			checkpoint.setRoundNo(roundNo);
		}
	}

	public ArrayList<Checkpoint> getCheckpoints()
	{
		return checkpoints;
	}

	public Location getLocation()
	{
		return location;
	}

	public int getLocationNo()
	{
		return locationNo;
	}

	public int getRoundNo()
	{
		return roundNo;
	}

	public double getTime()
	{
		return calculateTime();
	}

	public void setCheckpoints(ArrayList<Checkpoint> checkpoints)
	{
		this.checkpoints = checkpoints;
	}

	public void setLocation(Location location)
	{
		this.location = location;
		this.setLocationNo(location.getLocationNo());
	}

	public void setLocationNo(int locationNo)
	{
		this.locationNo = locationNo;
	}

	public void setRoundNo(int roundNo)
	{
		this.roundNo = roundNo;
	}
}